package com.weipu.common.subject;

import java.util.ArrayList;
import java.util.List;

import com.weipu.common.observer.MessageObServer;

public class MessageSubjectImpl implements MessageSubject {
	private static MessageSubjectImpl myMessageSubject;
	private List<MessageObServer> observers=new ArrayList<MessageObServer>();
	
	private MessageSubjectImpl(){
		
	}
	/**
	 * 懒汉单例
	 * @return
	 */
	public static MessageSubjectImpl getMessageSubjectImplInstance(){
		if(null==myMessageSubject){
			myMessageSubject=new MessageSubjectImpl();
		}
		
		return myMessageSubject;
	}
	
	@Override
	public void attach(MessageObServer mObserver) {
        observers.add(mObserver);
	}

	@Override
	public void remove(MessageObServer mObserver) {
       observers.remove(mObserver);
	}

	@Override
	public void notice(int newMsgNum,int msgType) {
        for (int i = 0; i < observers.size(); i++) {
			observers.get(i).notifyMessageChange(newMsgNum,msgType);
		}
	}

}
